package com.example.comiccenter.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.example.comiccenter.domain.User;
import com.example.comiccenter.service.UserService;
import com.example.comiccenter.utils.JWTUtil;
import com.example.comiccenter.utils.ResultData;
import com.example.comiccenter.utils.SearchBean;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

import static com.example.comiccenter.utils.Consts.*;

@RestController
@RequestMapping(value = "/user",method = RequestMethod.POST)
public class UserController {
    @Resource
    private UserService userservice;
    //管理
    @PostMapping(value = "/insert")
    public ResultData insertUser(@RequestBody User user) {
        if (user.getPassword() == null){
            user.setPassword("123456");
        }
        if(user.getAvatar() == null){
            user.setAvatar("/user/avatar/defaultavatar.jpg");
        }
        return userservice.insertUser(user);
    }


    //加载
    @PostMapping(value = "/loadpage")
    public ResultData selectUser(@RequestBody SearchBean<User> searchBean){
        return userservice.loadAAllUsers(searchBean);
    }

    //删除
    @DeleteMapping(value = "/{username}")
    public ResultData delete(@PathVariable String username){
        return userservice.deleteUser(username);
    }

    //修改信息
    @PutMapping(value = "/edituser")
    public ResultData edit(@RequestBody User user){
        return userservice.updateUser(user);
    }
    //更新头像
    @PostMapping(value = "/updateAvator")
    public ResultData update(@RequestParam("file") MultipartFile avatorFile,@RequestParam("id") Integer id){
        if (avatorFile.isEmpty()){
            return ResultData.error(400,"文件为空");
        }
        //文件名=当前时间+文件原名
        String FileName = System.currentTimeMillis()+ avatorFile.getOriginalFilename();
        //文件存储路径
        String FilePath = rootPath + "user\\avatar\\";
        String targetFilePath = targetRootPath + "user\\avatar\\";
        File file1 = new File(FilePath);
        if (!file1.exists()){
            file1.mkdir();
        }
        //实际文件路径
        File dest = new File(FilePath+FileName);
        File targetDest = new File(targetFilePath+FileName);
        //数据库更新
        String DbAvatarStorage = "/images/user/avatar/"+FileName;
        try {
            FileUtils.copyInputStreamToFile(avatorFile.getInputStream(),dest);
            FileUtils.copyInputStreamToFile(avatorFile.getInputStream(),targetDest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        User user = new User();
        user.setId(id);
        user.setAvatar(DbAvatarStorage);
        ResultData resultData = userservice.updateUser(user);
        resultData.setData(DbAvatarStorage);
        return resultData;
    }
/*
*
* 客户端
*
* */
    //客户端登录
    @RequestMapping(value="/userlogin")
    public ResultData login(@RequestBody User user){
        return userservice.userLogin(user);
    }
    //头像获取

}
